<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description: 第三方服务
 *
 * @author      Eden
 * @datetime    2016/12/7 17:56
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class Service extends ADM_Controller
{

    private $keys = array('qq', 'wechat', 'service', 'stream');

    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->load->view(parent::TMP_DIR . '/system/service.html');
    }

    public function getConfig()
    {
        $send['state'] = false;
        do{
            $type = $this->input->get('type');
            if (!empty($type) && !in_array($type, $this->keys)) {
                $send['error'] = '无此配置';
                break;
            }

            $this->load->model('admin/system/Setting_Model', 'setting');
            $search = !empty($type) ? array($type) : $this->keys;
            $result = $this->setting->get('system', $search);

            if ($result) {
                $send['state'] = true;
                $send['data'] = $result;
                break;
            }

            $send['error'] = '暂无数据';
        } while(false);
        $this->output->send($send);
    }

    public function saveConfig()
    {
        $send['state'] = false;
        do {

            if ($this->input->method(true) !== 'POST') {
                $send['error'] = '非法请求';
                break;
            }

            $key = $this->input->get('key');
            $params = $this->input->post();
            if (empty($key) || empty($params) || !in_array($key, $this->keys)) {
                $send['error'] = '参数错误';
                break;
            }

            $this->load->model('admin/system/Setting_Model', 'setting');
            if ($res = $this->setting->save($key, $params)) {
                $send['state'] = true;
            } else {
                $send['error'] = '保存失败';
            }

        } while(false);
        $this->output->send($send);
    }
}
